///////////////////////////////////////////////////////////
// 
//  MyBox.cpp
// 
//  Authors:  Jeremy Bennett
//
///////////////////////////////////////////////////////////

// Local Includes
#include "stdafx.h"
#include "MyTriangle.h"
#include "MyContext.h"

// SDL Includes
#include <SDL_opengl.h>

// Bullet Includes
#include <BulletCollision/CollisionShapes/btCylinderShape.h>
#include <BulletCollision\CollisionShapes\btConvexHullShape.h>
#include <BulletCollision\CollisionShapes\btCompoundShape.h>
#include <BulletCollision\CollisionShapes\btTriangleShape.h>
#include <LinearMath/btDefaultMotionState.h>

// System Includes
#include  <math.h>
#define PI 3.141592653589793238

MyTriangle::MyTriangle( const btScalar vScale, 
                        const btScalar vThickness, 
                        const btVector3 &vOrigin, 
                        const btScalar   fMass ) 
: MyShape() 
{
    _eType = my_triangle;

    //triangle origin
	btScalar x = vOrigin.x(),
		     y = vOrigin.y(),
	         z = vOrigin.z();

	btScalar tempx = vScale/2.0f,
			 tempy = (cos(30.0f*PI/180.0f)*vScale)/2.0f,
			 tempz = vThickness/2.0f;

	/*btVector3 points[6] = {btVector3(	x - tempx,		y - tempy,		0.0f),
						   btVector3(	x + tempx,		y - tempy,		0.0f),
						   btVector3(	x,				y + tempy,		0.0f),
						   btVector3(	x - tempx,		y - tempy,		vThickness),
						   btVector3(	x + tempx,		y - tempy,		vThickness),
						   btVector3(	x,				y + tempy,		vThickness)};*/

	
    /*
	btVector3 points[9] = {btVector3(	x - tempx,		y - tempy,		0.0f),
						   btVector3(	x + tempx,		y - tempy,		0.0f),
						   btVector3(	x,				y + tempy,		0.0f),
						   btVector3(	x - tempx,		y - tempy,		4.0f),
						   btVector3(	x + tempx,		y - tempy,		4.0f),
						   btVector3(	x,				y + tempy,		4.0f),
						   btVector3(	x - tempx/2.0f,			y,		4.0f),
					       btVector3(	x + tempx/2.0f,			y,		4.0f),
						   btVector3(	x,				y - tempy,		4.0f)};
    */

	/*btVector3 points[9] = {btVector3(	x - tempx,		y - tempy,		z - tempz),
						   btVector3(	x + tempx,		y - tempy,		z - tempz),
						   btVector3(	x,				y + tempy,		z - tempz),
						   btVector3(	x - tempx,		y - tempy,		z + tempz),
						   btVector3(	x + tempx,		y - tempy,		z + tempz),
						   btVector3(	x,				y + tempy,		z + tempz),
						   btVector3(	x - tempx/2.0f,			y,		z + tempz),
					       btVector3(	x + tempx/2.0f,			y,		z + tempz),
						   btVector3(	x,				y - tempy,		z + tempz)};*/
	
	/*btVector3 points[6] = {btVector3(	x - tempx,		y - tempy,		z - tempz),
						   btVector3(	x + tempx,		y - tempy,		z - tempz),
						   btVector3(	x,				y + tempy,		z - tempz),
						   btVector3(	x - tempx,		y - tempy,		z + tempz),
						   btVector3(	x + tempx,		y - tempy,		z + tempz),
						   btVector3(	x,				y + tempy,		z + tempz)};*/

	
	/*btVector3 points[6] = {btVector3(0.0f, 0.0f, 0.0f),
						   btVector3(vScale, 0.0f, 0.0f),
						   btVector3((vScale / 2.0f), btScalar(cos(30.0f*PI/180.0f))*vScale, 0.0f),
	                       btVector3(0.0f, 0.0f, vThickness),
						   btVector3(vScale, 0.0f, vThickness),
						   btVector3((vScale / 2.0f), btScalar(cos(30.0f*PI/180.0f))*vScale, vThickness)};*/

	btConvexHullShape* triangle = new btConvexHullShape();
    triangle->addPoint(btVector3(x - tempx,		y - tempy,		vThickness*-1.0f));
    triangle->addPoint(btVector3(x + tempx,		y - tempy,		vThickness*-1.0f));
    triangle->addPoint(btVector3(x,				y + tempy,		vThickness*-1.0f));
    triangle->addPoint(btVector3(x - tempx,		y - tempy,		vThickness*-0.5f));
    triangle->addPoint(btVector3(x + tempx,		y - tempy,		vThickness*-0.5f));
    triangle->addPoint(btVector3(x,				y + tempy,		vThickness*-0.5f));
    triangle->addPoint(btVector3(x - tempx,		y - tempy,		vThickness* 0.0f));
    triangle->addPoint(btVector3(x + tempx,		y - tempy,		vThickness* 0.0f));
    triangle->addPoint(btVector3(x,				y + tempy,		vThickness* 0.0f));
    triangle->addPoint(btVector3(x - tempx,		y - tempy,		vThickness* 0.5f));
    triangle->addPoint(btVector3(x + tempx,		y - tempy,		vThickness* 0.5f));
    triangle->addPoint(btVector3(x,				y + tempy,		vThickness* 0.5f));
    triangle->addPoint(btVector3(x - tempx,		y - tempy,		vThickness* 1.0f));
    triangle->addPoint(btVector3(x + tempx,		y - tempy,		vThickness* 1.0f));
    triangle->addPoint(btVector3(x,				y + tempy,		vThickness* 1.0f));


	/*btConvexHullShape* triShape = new btConvexHullShape(&points[0].getX(),6);
	btTransform lclTran;
	lclTran.setIdentity();

	btCompoundShape* triangle = new btCompoundShape();
	triangle->addChildShape(lclTran, triShape);
	*/


	_pShp = triangle;
	_vCol = btVector4(0.0f, 0.0f, 1.0f, 1.0f);

    btTransform colTran;
	colTran.setIdentity();
    colTran.setOrigin(vOrigin);

    btVector3 locInertia(0,0,0);
    if ( fMass != 0 ) {
         _pShp->calculateLocalInertia(fMass,locInertia);
    }

    btDefaultMotionState* pMotState = new btDefaultMotionState(colTran);
    btRigidBody::btRigidBodyConstructionInfo rbInfo(fMass,pMotState,_pShp,locInertia);
	rbInfo.m_friction = 4.0f;
	//rbInfo.m_angularDamping = 1.0f;
	//btRigidBody::btRigidBodyConstructionInfo rbInfo(fMass,pMotState,triShape,locInertia);
    setupRigidBody(rbInfo);

    if ( fMass != 0 ) {
        setLinearFactor(btVector3(1.0f, 1.0f, 1.0f));
	    setAngularFactor(btVector3(1.0f, 1.0f, 1.0f));
	    setCcdMotionThreshold(1.);
	    setCcdSweptSphereRadius(0.2f);
    }
}

MyTriangle::~MyTriangle() 
{
}

bool 
MyTriangle::render( MyContext *ctx )
{
    // TOIXC: NYI:

    //return true;
    btScalar	m[16];

    btDefaultMotionState *pMotState = (btDefaultMotionState*)getMotionState();
    pMotState->m_graphicsWorldTrans.getOpenGLMatrix(m);

    glEnable(GL_COLOR_MATERIAL);
    glColor4f( _vCol[0], _vCol[1], _vCol[2], _vCol[3] );

#if 1
    glPushMatrix(); 
	glMultMatrixf(m);

	const btConvexHullShape* triangle = static_cast<const btConvexHullShape*>(_pShp);
	const btVector3* points = triangle->getUnscaledPoints();

	glBegin(GL_POLYGON);
		glVertex3fv(points[0]);
		glVertex3fv(points[1]);
		glVertex3fv(points[2]);
	glEnd();

    glBegin(GL_POLYGON);
        glVertex3fv(points[0]);
		glVertex3fv(points[1]);
		glVertex3fv(points[12]);
		glVertex3fv(points[13]);
	glEnd();

    glBegin(GL_POLYGON);
        glVertex3fv(points[1]);
		glVertex3fv(points[2]);
		glVertex3fv(points[13]);
		glVertex3fv(points[14]);
	glEnd();

    glBegin(GL_POLYGON);
        glVertex3fv(points[2]);
		glVertex3fv(points[0]);
		glVertex3fv(points[14]);
		glVertex3fv(points[12]);
	glEnd();

    glBegin(GL_POLYGON);
		glVertex3fv(points[12]);
		glVertex3fv(points[13]);
		glVertex3fv(points[14]);
	glEnd();

    /*
	glBegin(GL_POLYGON);
			glVertex3f(points[0].x(), points[0].y(), points[0].z());
			glVertex3f(points[2].x(), points[2].y(), points[2].z());
			glVertex3f(points[3].x(), points[3].y(), points[3].z());
			glVertex3f(points[5].x(), points[5].y(), points[5].z());
	glEnd();

	glBegin(GL_POLYGON);
			glVertex3f(points[1].x(), points[1].y(), points[1].z());
			glVertex3f(points[2].x(), points[2].y(), points[2].z());
			glVertex3f(points[4].x(), points[4].y(), points[4].z());
			glVertex3f(points[5].x(), points[5].y(), points[5].z());
	glEnd();

	glBegin(GL_POLYGON);
			glVertex3f(points[0].x(), points[0].y(), points[0].z());
			glVertex3f(points[1].x(), points[1].y(), points[1].z());
			glVertex3f(points[3].x(), points[3].y(), points[3].z());
			glVertex3f(points[4].x(), points[4].y(), points[4].z());
	glEnd();

	glBegin(GL_POLYGON);
		glVertex3f(points[3].x(), points[3].y(), points[3].z());
		glVertex3f(points[4].x(), points[4].y(), points[4].z());
		glVertex3f(points[5].x(), points[5].y(), points[5].z());
	glEnd();
    */

    glPopMatrix();
#endif

    return true;
}